package com.haidong;

import com.haidong.mapper.UserMapper;
import com.haidong.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * MyBatis代理开发
 */
public class MyBatisDemo2 {
    public static void main(String[] args) throws Exception {
        //1.加载mybatis核心配置文件,获取SqlsessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取sqlsession对象,用来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.执行sql
        //获取UserMapper接口的代理对象
        UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = usermapper.selectAll();

        System.out.println(users);

         //4.释放资源
        sqlSession.close();
    }
}
